package com.xiaoquan.gulimall.coupon.service.impl;

import com.xiaoquan.common.to.MemberPrice;
import com.xiaoquan.common.to.SkuReductionTo;
import com.xiaoquan.gulimall.coupon.entity.MemberPriceEntity;
import com.xiaoquan.gulimall.coupon.entity.SkuLadderEntity;
import com.xiaoquan.gulimall.coupon.service.MemberPriceService;
import com.xiaoquan.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoquan.common.utils.PageUtils;
import com.xiaoquan.common.utils.Query;

import com.xiaoquan.gulimall.coupon.dao.SkuFullReductionDao;
import com.xiaoquan.gulimall.coupon.entity.SkuFullReductionEntity;
import com.xiaoquan.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * sku的满减，优惠信息
     * @param skuReductionTo
     */
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
   // sku的优惠、满减等信息  跨表查询 gulimall-sms -->sms_sku_ladder、sms_sku_full_reduction、sms_member_price
        // 1、保存满减打折，会员价   sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId()); // sku id
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount()); // 满几件
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount()); // 打几折
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus()); // 折后价
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        // 2、保存 sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuFullReductionEntity.getSkuId());  // sku id
        skuFullReductionEntity.setFullPrice(skuFullReductionEntity.getFullPrice());  // 满多少
        skuFullReductionEntity.setReducePrice(skuFullReductionEntity.getReducePrice()); // 减多少
        skuFullReductionEntity.setAddOther(skuFullReductionEntity.getAddOther());  // 是否参与其他优惠
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("o")) == 1){
            this.save(skuFullReductionEntity);
        }

        // 3、保存 sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
            memberPriceEntity.setMemberLevelId(item.getId());
            memberPriceEntity.setMemberLevelName(item.getName());
            memberPriceEntity.setMemberPrice(item.getPrice());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(collect);
    }

}